﻿using System;

namespace Centido.Core
{
    /// <summary>
    ///   A set of delete all operations.
    /// </summary>
    public static class TableDeleteAllOperations
    {
        /// <summary>
        ///   Deletes all rows in the table.
        /// </summary>
        private static void DeleteAllRowsImplementation(Table table)
        {
            table.Domain.DbAccess.ExecuteNonQuery(cmd => 
            {
                cmd.CommandText = string.Format("delete from {0}", table.FullName);
            });
        }



        /// <summary>
        ///   Deletes all rows in the table.
        /// </summary>
        /// <param name="table">The target table.</param>
        /// <remarks>
        /// <para>The current user has to have the delete permission on the table in order to execute this function.</para>
        /// </remarks>
        /// <exception cref="System.Data.Common.DbException">Thrown when the current user has no delete permission on the table.</exception>
        /// <example>
        ///   The following example shows how to delete all rows in the table.
        /// <code>
        ///     using System;
        ///     using Centido.Core;
        /// 
        ///     class Sample
        ///     {
        ///         static void Main()
        ///         {
        ///             var domainConfig = new DomainConfiguration()
        ///             {
        ///                 DbConnectionString = @"Driver={SQL Server};Server=.\SQLEXPRESS;Database=MyDb;Uid=admin;Pwd=P@ssw0rd;",
        ///                 DbProviderName = "MSSQLSERVER"
        ///             };
        ///         
        ///             var domain = Domain.Build(domainConfig);
        ///             
        ///             domain.DeleteAll();
        ///             
        ///             //Creating a new table
        ///             var table = domain.AddTable("my_table");
        /// 
        ///             //Granting all necessary permissions
        ///             table.GrantAll();
        ///
        ///             //Inserting some rows
        ///             Console.WriteLine("Inserting some rows");
        /// 
        ///             for (int i = 0; i &lt; 10; i++)
        ///                 table.InsertRow();
        ///             
        ///             Console.WriteLine("Row count = {0}", table.RowCount);
        ///             
        ///             //Deleting all rows from the table
        ///             Console.WriteLine("Deleting the row from the table view");
        ///             
        ///             table.DeleteAllRows();
        ///             
        ///             Console.WriteLine("Row count = {0}", table.RowCount);
        ///         }
        ///     } 
        /// </code>
        /// </example>
        public static void DeleteAllRows(this Table table)
        {
            if (table == null)
                throw new ArgumentNullException("table");

            DeleteAllRowsImplementation(table);
        }
    }
}
